package me.learningandroid.ch5_2.CustomContextedBroadcast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {
    private static String BC_ACTION="me.learningandroid.ch5_2.CustomContextedBroadcast.SEND";
    private static String BC_CLEAR="me.learningandroid.ch5_2.CustomContextedBroadcast.CLEAR";

    BroadcastReceiver broadcastReceiver1 = null;
    BroadcastReceiver broadcastReceiver2 = null;
    BroadcastReceiver broadcastReceiver3 = null;
    BroadcastReceiver broadcastReceiver4 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSend = findViewById(R.id.btnSend);
        Button btnSendOrdered = findViewById(R.id.btnSendOrdered);
        Button btnReset = findViewById(R.id.btnReset);

        CheckBox checkBox1 = findViewById(R.id.checkBox);
        CheckBox checkBox2 = findViewById(R.id.checkBox2);
        CheckBox checkBox3 = findViewById(R.id.checkBox3);

        // `IntentFilter`指定感兴趣的广播
        IntentFilter intentFilter = new IntentFilter(BC_ACTION);
        // 设置优先级，在有序广播中，高优先级先收到消息。
        intentFilter.setPriority(10);
        broadcastReceiver1 = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                checkBox1.setChecked(true);
            }
        };
        // 注册接收器
        registerReceiver(broadcastReceiver1, intentFilter);


        IntentFilter intentFilter2 = new IntentFilter(BC_ACTION);
        intentFilter2.setPriority(8);
        broadcastReceiver2 = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                checkBox2.setChecked(true);
                // 有序广播从高优先级传向低优先级的接收器
                // 较高优先级的接收器调用`abortBroadcast`可以截断广播传递
                abortBroadcast();
            }
        };
        registerReceiver(broadcastReceiver2, intentFilter2);

        IntentFilter intentFilter3 = new IntentFilter(BC_ACTION);
        intentFilter3.setPriority(6);
        broadcastReceiver3 = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                checkBox3.setChecked(true);
            }
        };
        registerReceiver(broadcastReceiver3, intentFilter3);

        btnSend.setOnClickListener(v-> {
            sendBroadcast(new Intent(BC_ACTION));
        });
        btnSendOrdered.setOnClickListener(v-> {
            // 发送有序的广播只需要调用`activity.sendOrderedBroadcast`方法
            sendOrderedBroadcast(new Intent(BC_ACTION), null/*permission*/);
        });


        IntentFilter intentFilter4 = new IntentFilter(BC_CLEAR);
        broadcastReceiver4 = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                checkBox1.setChecked(false);
                checkBox2.setChecked(false);
                checkBox3.setChecked(false);
            }
        };
        // `LocalBroadcastManager`专门用于应用内广播
        // 使用时获取单例`LocalBroadcastManager.getInstance(context)`后，操作和系统广播类似，
        // `LocalBroadcastManager.registerReceiver`注册
        // `LocalBroadcastManager.sendBroadcast`发送
        // `LocalBroadcastManager.unregisterReceiver`取消注册
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver4, intentFilter4);
        btnReset.setOnClickListener(v-> {
            // 本地广播发送
            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BC_CLEAR));
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // `broadcastReceiver`在注册后，必须在对应的生命周期函数里解除注册，否则会占用系统资源。
        unregisterReceiver(broadcastReceiver1);
        unregisterReceiver(broadcastReceiver2);
        unregisterReceiver(broadcastReceiver3);
        unregisterReceiver(broadcastReceiver4);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver4);
    }
}